import i18n from "../i18n/index.js";
// 回调对象
export const CALLBACKS = "hcbs";

export function isUndef(v) {
  return v === undefined || v === null;
}

// 数据转换以及异常处理，并返回处理后的数据
export function jsonParse(res) {
  let data = {};
  try {
    data = JSON.parse(res);
  } catch (err) {
    try {
      let dataStr = res.replace(/:"{/g, ":{");
      dataStr = dataStr.replace(/}",/g, "},");
      dataStr = dataStr.replace(/\\/g, "");
      dataStr = dataStr.replace(/\n/g, "");
      dataStr = dataStr.replace(/\r/g, "");
      dataStr = dataStr.replace(/\t/g, "");
      data = JSON.parse(dataStr);
    } catch (error) {
      try {
        let dataStr = res.replace(/:"{/g, ":{");
        dataStr = dataStr.replace(/\\/g, "");
        dataStr = dataStr.replace(/(^\s*)|(\s*$)/g, "");
        dataStr = dataStr.replace(/}",/g, "},");
        dataStr = dataStr.replace(/}"/g, "}");
        data = JSON.parse(dataStr);
      } catch (error) {
        let dataStr = res.replace(/:"{/g, ":{");
        dataStr = dataStr.replace(/\\/g, "");
        dataStr = dataStr.replace(/}",/g, "},");
        dataStr = dataStr.replace(/}"/g, "}");
        dataStr = dataStr.replace(/"{/g, "{");
        dataStr = dataStr.replace(/]"/g, "]");
        dataStr = dataStr.replace(/\["/g, "[");
        dataStr = dataStr.replace(/\\/g, "");
        dataStr = dataStr.replace(/\n/g, "");
        dataStr = dataStr.replace(/\r/g, "");
        dataStr = dataStr.replace(/\t/g, "");
        dataStr = dataStr.replace(/(^\s*)|(\s*$)/g, "");
        dataStr = dataStr.replace(/}"/g, "}");
        try {
          data = JSON.parse(dataStr);
        } catch (error) {
          try {
            console.hLog("数据解析错误！", res);
          } catch (error) {}
        }
      }
    }
  }
  return data;
}

/**
 * try catch 函数集成精简
 * @param {Function} fn 需要异常处理的函数
 * @param {String} warnMsg 提示信息
 * @param {Function} fn2 异常之后执行的异常处理函数
 */
export function tryFn(fn, warnMsg, fn2) {
  try {
    fn();
  } catch (err) {
    fn2 && fn2();
    let warnMsgs = warnMsg || "未自定义的";
    console.warn(`${warnMsgs}错误信息: ${err}`);
  }
}

export function addZero(num) {
  return `0${num}`.slice(-2);
}

/**
 * 本地时间转UTC时间
 * @param {Date} date
 * @param {Boolean} deviceTime true:设备对时UTC时间  false:正常UTC时间
 * @returns UTC格式时间
 */
export function formatUTCDate(date, deviceTime) {
  let year = date.getUTCFullYear();
  let month = date.getUTCMonth() + 1;
  let day = date.getUTCDate();
  let hour = date.getUTCHours();
  let minutes = date.getUTCMinutes();
  let seconds = date.getUTCSeconds();

  return deviceTime
    ? // yyMMddHHmmss
      `${String(year).slice(2)}${addZero(month)}${addZero(day)}${addZero(
        hour
      )}${addZero(minutes)}${addZero(seconds)}`
    : // 返回当前当前时间yyyyMMddTHHmmssZ格式
      `${year}${addZero(month)}${addZero(day)}T${addZero(hour)}${addZero(
        minutes
      )}${addZero(seconds)}Z`;
}

/**
 * 四舍五入
 * @param {Number/String} number 数字
 * @param {Number} precision 位数
 */
export function round(number, precision) {
  return Math.round(Number(number) + "e" + precision) / Math.pow(10, precision);
}

export function initState(profile) {
  let states = {};
  for (let key in profile) {
    states[key] = {};
    let item = profile[key];
    if (Array.isArray(item)) {
      item.forEach((arrayItem, index) => {
        states[key][arrayItem.name] = arrayItem.default;
      });
    } else {
      states[key][item.name] = item.default;
    }
  }
  return states;
}

export function handleState(profile, state, result) {
  if (!result || !result.sid || !result.data) {
    return;
  }

 
  let sid = result.sid;

  for (let key in profile) {
    if (key !== sid) {
      continue;
    }

    let data = result.data;
    for (let char in data) {
      state[sid][char] = data[char];
    }
  }
}

export function generatePromise(args = {}) {
  //框架参数及回调方法
  let {
    type,
    method,
    params = [],
    callback,
    onCallback,
    afterHilinkCalled
  } = args;

  return new Promise((resolve, reject) => {
    try {
      //判断当前的hilink接口是否存在，method为hilink接口名
      if (!window.hilink || !window.hilink[method]) {
        console.hLog(`No ${method}`);
        return;
      }
      if (type === "sync") {
        let value = window.hilink[method](...params);
        resolve(value);
      } else {
        //判断回调方法名
        let callbackName = callback ? callback : `${method}Callback`;
        //解析回调数据
        window[CALLBACKS][callbackName] = res => {
          let result = jsonParse(res);
          resolve(result);
          if (onCallback && typeof onCallback === "function") {
            onCallback(result);
          }
        };
        window.hilink[method](...params, `${CALLBACKS}.${callbackName}`);
        if (afterHilinkCalled && typeof afterHilinkCalled === "function") {
          afterHilinkCalled();
        }
      }
    } catch (err) {
      console.hLog(err);
    }
  });
}

export function generateWifiPromise(args = {}) {
  //框架参数及回调方法
  let { method, params=[], callback } = args;

  return new Promise((resolve, reject) => {
    try {
      //判断当前的hilink接口是否存在，method为hilink接口名
      if (!window.hilink || !window.hilink[method]) {
        console.hLog(`No ${method}`);
        return;
      }

      //判断回调方法名
      let callbackName = callback ? callback : `${method}Callback`;
      console.log("callbackName:",callbackName)
      //解析回调数据
      window[CALLBACKS][callbackName] = res => {
        let result = jsonParse(res);
        resolve(result);
      };
      console.log("...params:",...params)
      window.hilink[method](...params, `${CALLBACKS}.${callbackName}`);
    } catch (err) {
        console.log("错误")
      console.hLog(err);
    }
  });
}
